nameSpace("browse");

/**
 * 条件
 * 
 * @type
 */
browse.condition = {
	orderType : 0,
	lableName : '',
	clickLableId : 'lable_0',
	pageSize : 60,
	pageNo : 1
};

/**
 * 点击标签 加载图片列表
 * 
 * @param {string}
 *            lableId
 * @param {string}
 *            lableName
 */
browse.clickLabel = function(lableId, lableName) {
	if (lableId == browse.condition.clickLableId) {
		return;
	}
	$('#' + browse.condition.clickLableId).removeClass('selected');
	$('#' + lableId).addClass('selected');
	browse.condition.clickLableId = lableId;
	browse.condition.lableName = lableName;
	browse.condition.pageNo = 1;
	if(lableId == 'lable_0') {
		$('#selectLableId').html('全部');
		$('#lableTietiaoId').hide();
	} 
	else {
		
		$.ajax({
			url:'tag_tag_checkisAdd.htm',
			type: "POST",
	   		dataType:'text',
	   		data:{lableName:lableName},
	   		async:false,
	   		success: function(data){
			   if(data == 'true') {
			   		$('#selectLableId').html(lableName);
					$('#lableTietiaoId').removeClass('follow').addClass('followed').attr('onclick',"javascript:browse.attendLable('cancel')");
					$('#lableTietiaoId').show();
			   }
			   else {
			   		$('#selectLableId').html(lableName);
					$('#lableTietiaoId').removeClass('followed').addClass('follow').attr('onclick',"javascript:browse.attendLable('add')");
					$('#lableTietiaoId').show();
			   }
			}
		});
		
	
	}
	browse.loadCommodityInfos();
};

/**
 * 排序
 * 
 * @param {}
 *            orderSortId
 */
browse.order = function(orderSortId) {
	if (orderSortId == 'hot_sort_id') {
		$('#time_sort_id').removeClass('sortselected');
		$('#time_sort_id').removeClass('ascending');
		if ($('#' + orderSortId).hasClass('sortselected')) {
			if ($('#' + orderSortId).hasClass('ascending')) {
				browse.condition.orderType = 0;
				$('#' + orderSortId).removeClass('ascending');
			} else {
				browse.condition.orderType = 1;
				$('#' + orderSortId).addClass('ascending');
			}
		} else {
			$('#' + orderSortId).addClass('sortselected');
			browse.condition.orderType = 0;
			$('#' + orderSortId).removeClass('ascending');
		}
	} else {
		$('#hot_sort_id').removeClass('sortselected');
		$('#hot_sort_id').removeClass('ascending');
		if ($('#' + orderSortId).hasClass('sortselected')) {
			if ($('#' + orderSortId).hasClass('ascending')) {
				browse.condition.orderType = 2;
				$('#' + orderSortId).removeClass('ascending');
			} else {
				browse.condition.orderType = 3;
				$('#' + orderSortId).addClass('ascending');
			}
		} else {
			$('#' + orderSortId).addClass('sortselected');
				browse.condition.orderType = 2;
				$('#' + orderSortId).removeClass('ascending');
		}
	}

	browse.loadCommodityInfos();
};

/**
 * 加载列表数据
 */
browse.loadCommodityInfos = function() {
	$("#commodity_infos_id")
			.html('<div class="pagination"><span class="loading" >正在加载数据...</span></a></div>');
	$.ajax({
		type : "POST",
		dataType : 'html',
		data : browse.condition,
		cache : false,
		url : "commodity_browse_loadCommodityInfos.htm",
		success : function(data) {
			$("#commodity_infos_id").html(data);
			$("#commodity_infos_id img").lazyload({
						effect : "fadeIn"
					});
			var len = $('#browse_size').val();
			if (len == browse.condition.pageSize) {
				$('#browse_size').remove();
				browse.condition.pageNo = browse.condition.pageNo + 1;
				$('#commodity_infos_id')
						.append('<div class="pagination" id="browse_nextpage'
								+ browse.condition.pageNo
								+ '"><span class="loading" >正在加载数据...</span></a></div>');
				browse.next();
			}
		}
	});
};

browse.next = function() {
	$("#browse_nextpage" + browse.condition.pageNo).each(function() {
		$(this).one("appear", function() {
			$.ajax({
				type : "POST",
				dataType : 'html',
				data : browse.condition,
				cache : false,
				url : "commodity_browse_loadCommodityInfos.htm",
				success : function(data) {
					$("#commodity_infos_id")
							.append('<div id="nextpage'
									+ browse.condition.pageNo + '" >' + data
									+ '</div>');
					$("#browse_nextpage" + browse.condition.pageNo).remove();
					$('#nextpage' + browse.condition.pageNo + ' img').lazyload(
							{
								effect : "fadeIn"
							});

					var len = $('#browse_size').val();
					if (len == browse.condition.pageSize) {
						$('#browse_size').remove();
						browse.condition.pageNo = browse.condition.pageNo + 1;
						$('#commodity_infos_id')
								.append('<div class="pagination" id="browse_nextpage'
										+ browse.condition.pageNo
										+ '"><span class="loading" >正在加载数据...</span></a></div>');

						browse.next();
					}
				}
			});
		});
	});

	$(window).bind("scroll", function(event) {
		$("#browse_nextpage" + browse.condition.pageNo).each(function() {

			if ($(window).height() + $(window).scrollTop() > $(this).offset().top) {
				$(this).trigger("appear");
			}
		});
	});
};

/**
 * 撕条操作 单个撕条
 * 
 * @param {}
 *            commodityId
 */
browse.shitiao = function(comId) {
	com.tietiao.shitiao(comId, function(suc, msg) {
				if (suc == 'true') {
					var tieNum = $('#tietiao_num_' + comId).html();
					tieNum = tieNum.substring(1, tieNum.length);
					tieNum = new Number(tieNum) - 1;
					$('#tietiao_num_' + comId).html('+' + tieNum);
					$('#tie_shi_' + comId).html('<span></span>贴条');
					$('#tie_shi_' + comId).attr(
							'href',
							'javascript:com.tietiao.showTietiaos("show_tie_'
									+ comId + '",' + comId + ')');
					$('#tie_shi_' + comId)
							.attr('class', 'button button-follow');
				} else {
					com.tietiao.timeAlert(msg, 1, true);
				}
			}, function() {
				com.tietiao.timeAlert('撕条失败，请稍候再试...', 1, true);
			});
};

browse.attendLable = function(type){
	$.ajax({
	   type: "POST",
	   dataType:'json',
	   cache:false,
	   url: "tag_tag_addOrCanel.htm",
	   data:{type:type,lableName:$("#selectLableId").html(),time:new Date()},
	   success:function(data){
	   	 if(data.result){
	   	 	if('cancel'==type){
	   	 		$(".wrapper-content .addorcancel").removeClass("followed").addClass("follow").attr('onclick',"browse.attendLable('add')");
	   	 	}else{
	   	 		$(".wrapper-content .addorcancel").removeClass("follow").addClass("followed").attr('onclick',"browse.attendLable('cancel')");
	   	 	}
	   	 }else{
	   	 	alert("处理失败，请重新再试");
	   	 }
	   }
	});
}


$(document).ready(function() {
			browse.loadCommodityInfos();
		});
